#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include <string.h>



int main(int argc,char **argv)
{

	
	Stack stack;

	Stack optStack;

	char pInput[64]={0};

	char cc=0;
	
	int inputlen=0;

	int i;

	int calNum1,calNum2,calResult;

	Item *pTmp=NULL;
	Item *pNumber1=NULL,*pNumber2=NULL;


	StackInit(&stack);
	StackInit(&optStack);


	void IteratorStack(Stack *pStack);


	if(argc<2)
	{
		
		printf("input the string\n");
		return 0;
	}

	inputlen=strlen(*(argv+1));


	for(i=0;i<inputlen;i++)
	{
	
		cc=*(*(argv+1)+i);
		switch(cc)
		{
		
			case '+':
			case '-':
			case '*':
			case '/':

				pTmp=(Item *)malloc(sizeof(Item));
                                pTmp->element=atoi(pInput);
                                pTmp->pNext=NULL;
                                StackPush(pTmp,&stack); 
                                IteratorStack(&stack);
				pInput[0]=0;

				
				pTmp=(Item *)malloc(sizeof(Item));
                                pTmp->element=(int)cc;
                                pTmp->pNext=NULL;
                                StackPush(pTmp,&optStack);			
				IteratorStack(&optStack);
				break;
			default:

				strncat(pInput,&cc,1);
				break;
		
		}
	}



	inputlen=strlen(*(argv+1));

	if(inputlen>0)
	{
	
			 	pTmp=(Item *)malloc(sizeof(Item));
                                pTmp->element=atoi(pInput);
                                pTmp->pNext=NULL;
                                StackPush(pTmp,&stack);
                                IteratorStack(&stack);
	}


	
	
		while((pTmp=StackPop(&optStack))!=NULL)
		{
		
			calNum1=(pNumber1=StackPop(&stack))->element;
			calNum2=(pNumber2=StackPop(&stack))->element;
			
			switch((char)pTmp->element)
			{
				case '+':
					calResult=calNum2+calNum1;
					break;
				case '-':
					 calResult=calNum2-calNum1;
					 break;
				case '*':
					 calResult=calNum2*calNum1;
					 break;
				case '/':
					 calResult=calNum2/calNum1;
					 break;
			
			}
		printf("cal the result");


			 pTmp=(Item *)malloc(sizeof(Item));
                             	pTmp->element=calResult;
                                pTmp->pNext=NULL;
                                StackPush(pTmp,&stack);
                                IteratorStack(&stack);

			
		
		}
		


	
	


	/**
	do
	{

		cc=getchar();


		switch(cc)
		{
			case 's':
				scanf("%s",pInput);
				pTmp=(Item *)malloc(sizeof(Item));
				pTmp->element=atoi(pInput);
				pTmp->pNext=NULL;
				StackPush(pTmp,&stack);	
				IteratorStack(&stack);
				break;
			case 'p':

				pTmp=StackPop(&stack);
				if(pTmp!=NULL)
				{
					  printf("\n%d",pTmp->element);
				}


				IteratorStack(&stack);
				break;
			case '+':
			case '-':
				pTmp=(Item *)malloc(sizeof(Item));
                                pTmp->element=(int)'+'
                                pTmp->pNext=NULL;
                                StackPush(pTmp,&optStack);	


			case 'e':
				strncpy(pInput,"exit",4);
				break;
		
		
		}

	
	}while((strncmp("exit",pInput,4)!=0));


**/





	return 0;
}



 unsigned STACK_SIZE;
        unsigned CAPCITY;
        Item *item;


void StackInit(Stack *pStack)
{


	Item *pItem=NULL;
	if(pStack==NULL)
	{
		printf("the stack mem is NULL");
		return;
	}

	pStack->CAPCITY=8;
	pStack->STACK_SIZE=0;
	pStack->item=NULL;	
	


}





/**
 * push the item to stack
 */
void  StackPush(Item *item,Stack *pStack)
{

	Item *pTop=NULL;
	if(item==NULL)
	{
		return;
	}

	if(pStack==NULL)
	{
		return;
	}
	
	if(pStack->STACK_SIZE+1>=pStack->CAPCITY)
	{
		return;
	}


	pTop=pStack->item;

	if(pTop!=NULL)
	{
		item->pNext=pTop;
	}


	pStack->item=item;

	pStack->STACK_SIZE=pStack->STACK_SIZE+1;

}




void IteratorStack(Stack *pStack)
{

  	 Item *pItem=NULL;
         if(pStack==NULL)
         {
                return;
        }


        pItem=pStack->item;

        if(pItem==NULL)
        {

                pStack->STACK_SIZE=0;
                return;
        }

	printf("print the stack element\n\t");
	while(pItem!=NULL)
	{
	
	
		printf("%d\t",pItem->element);
		pItem=pItem->pNext;
	}

	printf("\n");

}


/**
 * pop the top element 
 */
Item *StackPop(Stack *pStack)
{

	 Item *pTop=NULL;
	 if(pStack==NULL)
	 {
                return NULL;
        }

	
	pTop=pStack->item;

	if(pTop==NULL)
	{
	
		pStack->STACK_SIZE=0;
		return NULL;
	}

	pStack->item=pTop->pNext;
	pStack->STACK_SIZE=pStack->STACK_SIZE-1;
	return pTop;	
}
